﻿
/**
 生产计划删除
 **/
function DelData(plCode,status) {
    var info = undefined;
    $.ajax({
        url: "../Plan/DelPlan",
        data: { plCode: plCode ,status:status},
        dataType: "json",
        type: "POST",
        async: false,
        success: function (data) {
            info = data;
        }
    });
    return info;
}
/**
* 存货选择
 * **/
function chooseProduct() {
    var pType = $("input[ name='plType']").val();
    if (pType == "P-BeiHuo") {
        let url = "../Basic/SelectInventory?isSingle=0";
        InfoOpenPage(url, "存货选择", "95%", "90%");
    }
}
/**
 * 存货参数获取
 * **/
function getChildPar(pars) {
    if (pars != undefined) {
        $("input[ name='proName']").val(pars[0].proName);
        $("input[ name='proCode']").val(pars[0].proCode);
        $("input[ name='spec']").val(pars[0].spec);
        //GetGyCode();
        GetBOMCode();
    }
}

/**
 * 根据存货、客户查询相关工艺
 * **/
function GetGyCode() {
    var proCode = $("input[ name='proCode']").val();
    var cuCode = $("input[ name='cuCode']").val();
    $.ajax({
        url: "../Plan/GetProductGyCode",
        data: { proCode: proCode, cuCode: cuCode },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                var data = rsl.data
                var info = data.gyName + "/" + data.gyCode;
                if (data.plCode != "") {
                    info += "/" + data.plCode;
                }
                $("input[ name='gyInfo']").val(info);
                $("input[ name='gyPlan']").val(data.plCode);
                $("input[ name='gyCode']").val(data.gyCode);
            } else {
                layer.msg(rsl.message);
            }
        }
    });
}

/**
 * 根据存货查询相关BOM
 * **/
function GetBOMCode() {
    var proCode = $("input[ name='proCode']").val();
    $.ajax({
        url: "../Plan/GetProductBOMCode",
        data: { proCode: proCode },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                var data = rsl.data
                $("input[ name='bomCode']").val(data.bomCode);

                GetBgByBOM(data.bomCode);
            } else {
                $("input[ name='errmsg']").val(rsl.message);  //没有bom不允许保存
                layer.msg(rsl.message);
            }
        }
    });
}


/**
 *选择客户 
 **/
function chooseCustomer() {
    var pType = $("input[ name='plType']").val();
    if (pType == "P-BeiHuo") {
        var url = "../Basic/SelectCustomer?isSingle=1&dataType=Cu";
        InfoOpenPage(url, "客户选择", "95%", "90%");
    }
}
/**
 * 加载获取客户数据信息
 **/
function getCustomPar(pars) {
    if (pars != undefined) {
        $("input[ name='cuName']").val(pars[0].cuName);
        $("input[ name='cuCode']").val(pars[0].cuCode);
    }
}
/**
* 加载计划信息
**/
function loadPlan(dataId) {
    $.ajax({
        url: "../Plan/getPlanInfoByCode",
        data: { plCode: dataId },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                $.each(rsl.data[0], function (name, value) {
                    if (name == "zhxBz" || name == "cjCode" || name == "cu_caizhi" || name == "gcName") {
                        $("#" + name + " option[value='" + value + "'] ").attr("selected", true);
                    }
                    else if (name == "orderMemo") {
                        document.getElementById('orderMemo').innerText = value;
                    }
                    else {
                        $("input[ name='" + name + "']").val(value);
                    }
                });

                layui.use('form', function () {
                    var form = layui.form;
                    form.render();
                });
               
                GetBgByBOM(rsl.data[0]["bomCode"]);

            } else {
                layer.msg(rsl.message);
            }
        }
    });
}


/**
 * 打开销售订单
 * **/
function OpenSale() {
    InfoOpenPage("../Plan/SalesInfo", "ERP销售订单", "95%", "90%");
}

/**
 * 获取销售订单数据
 **/
function getSaleOrderPar(pars) {
    //console.log(pars)
    if (pars != undefined) {
        $("input[ name='proName']").val(pars[0].proName);
        $("input[ name='proCode']").val(pars[0].proCode);
        $("input[ name='spec']").val(pars[0].spec);
        $("input[ name='plType']").val("P-Sale");
        $("input[ name='erpOrder']").val(pars[0].erpOrder);
        $("input[ name='erpRowId']").val(pars[0].erpRowId);
        $("input[ name='number']").val(pars[0].number);
        $("input[ name='cuCode']").val(pars[0].cuCode);
        $("input[ name='cuName']").val(pars[0].cuName);
        $("input[ name='jfDate']").val(pars[0].fDate);
        //$("input[ name='orderMemo']").val(pars[0].orderMemo);
        document.getElementById('orderMemo').innerText = pars[0].orderMemo;
        //GetGyCode();
        GetBOMCode();
    }
}

/**
 * 往期订单
 * **/
function ViewPlan() {
    let cuCode = $("input[ name='cuCode']").val();
    let cuName = $("input[ name='cuName']").val();
    let proCode = $("input[ name='proCode']").val();
    let proName = $("input[ name='proName']").val();
    InfoOpenPage("../Plan/RecentPlanInfo?cuCode=" + cuCode + "&proCode=" + proCode + "&cuName=" + cuName + "&proName=" + proName, "往期计划", "95%", "90%");
}

/**
 * 往期订单赋值
 **/
function lastPlanPar(pars) {
    if (pars != undefined) {
        $.each(pars[0], function (name, value) {
            if (name == "erpOrder") {
              //  $("input[ name='rePlCode']").val(value);
            } else {
                $("input[ name='" + name + "']").val(value);
            }

        });
        GetBOMCode();
        loadPlanGx(pars[0]["plCode"]);///////
        loadGx();
    }
}

/**
 * 预览工艺数据
 * **/
function ViewGY() {
    let plCode = $("input[ name='gyPlan']").val();
    let gyCode = $("input[ name='gyCode']").val();
    InfoOpenPage("../Plan/PlanProcess?plCode=" + plCode + "&read=1&gyCode=" + gyCode, "生产工艺(" + gyCode + ")", "95%", "90%");
}


/**
 * 保存生产计划
 * **/
function SavePlan(cData) {

    //console.log(sign)
    if (cData.length > 0) {
        if (!sign) {
            return;
        }
        sign = false;

        $.ajax({
            url: "../Plan/SaveProductPlan",
            data: { datas: JSON.stringify(FormToJson("dForm")), cData: JSON.stringify(cData) },
            type: "post",
            success: function (rsl) {
                if (rsl.result) {
                    layer.msg("计划保存成功", {icon:1});
                } else {
                    layer.msg(rsl.message, {icon:2});
                }

                setTimeout(function () {
                    sign = true;
                }, 3000); //单位是毫秒
                
            }
        });
    } else {
        layer.msg("请选择生产工序！", {icon:2});
        return;
    }
    
}

/**
 * 计划工艺数据保存
 * **/
function SavePlanGYData(formId) {
    var datas = JSON.stringify(FormToJson(formId));
    $.ajax({
        url: "../Plan/SavePlanTechnology",
        data: { datas: datas },
        type: "post",
        success: function (data) {
            if (data.result) {
                layer.msg("保存成功！", {icon:1});
            } else {
                layer.msg(data.message, {icon:2});
            }
        }
    });
}

/**
  * 加载计划工艺数据信息
  * **/
function loadGyFormData(plCode,gxCode) {
    $.ajax({
        url: "../Plan/GetPlanTechnologyForm",
        type: "post",
        data: { plCode: plCode, gxCode: gxCode },
        success: function (data) {
            if (data.result) {
                var datas = data.data;
                layui.use('form', function () {
                    var form = layui.form;
                    form.val('form-filter', datas);
                    form.render();
                });
            }
        }
    });
}

/**
* 计划工艺工序使用物料数据(空)
* **/
function SaveGxMaterials(proCodes, plCode, gxCode) {
    var obj = {
        proCodes: proCodes,
        plCode: plCode,
        gxCode: gxCode
    }
    $.ajax({
        url: "../Plan/savePlanMaterials",
        data: { datas: JSON.stringify(obj) },
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.result) {
                loadGxPro();
            } else {
                layer.msg(data.message, { icon: 2 });
            }
        }
    });
}
/**
 删除计划工序用料方法
 **/
function DelGxMaterialData(dataId) {
    var info = undefined;
    $.ajax({
        url: "../Plan/DelPlanMaterials",
        data: { dataId: dataId },
        dataType: "json",
        type: "POST",
        async: false,
        success: function (rsl) {

            info = rsl;
        }
    });
    return info;
}



/**
 工序用料
 * **/
function loadGxPro() {
    var mUrl = "../Plan/getDataList?dataType=planyl&condition=";
    let gxCode = $("input[ name='gxCode']").val();
    var obj = {
        plCode: plCode,
        gxCode: gxCode
    }
    layui.use('table', function () {
        var table = layui.table;
        var cols = table.render({
            elem: '#mplist'
            , url: mUrl + JSON.stringify(obj)
            , skin: 'row' //表格风格
            , even: true
            , page: false
            , limits: [50, 70, 100]
            , limit: 50 //每页默认显示的数量
            , cols: [[
                { field: 'proCode', title: '原材料编码', width: 120, event: "addPro" }
                , { field: 'proName', title: '原材料名称', width: 180, event: "addPro" }
                , { field: 'batch', title: '批次', width: 180 }
                , { field: 'cjStock', title: '车间库存', width: 120 }
                , { field: 'number', title: '累计数量', width: 120 }
                , { field: 'useNum', title: '使用数量', width: 120, edit: 'text' }
                , { field: 'right', align: 'center', title: '', toolbar: '#barDemo', width: 90 }
            ]], done: function (data) {


            }
        }).config.cols;

        //监听工具条
        table.on('tool(mplist)', function (obj) {
            var data = obj.data; //当前行旧数据
            var field = obj.field; //单元格字段
            var value = obj.value; //修改后的值
            var event = obj.event; //当前单元格事件属性值
            var update = {};
            update[field] = value;
            //把value更新到行中
            obj.update(update);
            if (event == "addPro") {
                rowObj = obj;
                if (rowObj.data["id"] == "") {
                    ChooseGxProduct();
                }

            }
            else if (event == 'del') {
                layer.confirm('确定要删除选中数据？', function (index) {
                    var rsl = DelGxMaterialData(data.id);
                    if (rsl.result) {
                        obj.del();
                        layer.msg("删除成功", {icon:1});
                    } else {
                        layer.msg(info.message, { icon: 2 });
                    }
                    layer.close(index);

                });
            } else if (event == 'view') {
                NewOpenPage("../Plan/PPMList?dataId=" + data.id, "详情", "95%", "90%", "list", "1", mUrl + plCode);
            }

        });

        //监听单元格编辑
        table.on('edit(mplist)', function (obj) {
            var field = obj.field;
            var data = obj.data;
            var value = obj.value;
            var oldValue = obj.oldValue;

            if (value == "") {
                value = "-";
            }


            if (field == "useNum") {

                if (isNaN(value)) {
                    layer.msg("使用数量必须为数字");
                    data[field] = oldValue;
                    obj.update(data);
                    return;
                }

                if (Number(value) <= 0) {
                    layer.msg("使用数量需大于0");
                    data[field] = oldValue;
                    obj.update(data);
                    return;
                }

                if (value != "" && data.uPrice != "") { //修改金额
                    updatePlanMaterials(data.id, field, value, null, null);
                }
            }
        });
    });

}
/**
 调整修改计划工序用料数据
 **/
function updatePlanMaterials(dataId, fields, value, proCode, number) {
    let gxCode = $("input[ name='gxCode']").val();
    var obj = {
        dataId: dataId,
        fieldName: fields,
        plCode: plCode,
        fieldValue: value,
        proCode: proCode,
        number: number,
        gxCode: gxCode
    }
    $.ajax({
        url: "../Plan/updatePlanMaterials",
        data: { datas: JSON.stringify(obj)},
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.result) {
                loadPro();
            } else {
                layer.msg(data.message, { icon: 2 });
            }
        }
    });
}

/**
保存工序剩料方法
 **/
function SaveShengLiao(dataId, value) {
    $.ajax({
        url: "../Plan/SaveShengLiao",
        data: { dataId: dataId, value: value },
        dataType: "json",
        type: "POST",
        success: function (data) {
            if (data.result) {
                layer.alert("保存成功", { icon: 6 });
            } else {
                layer.msg(data.message, { icon: 2 });
            }
        }
    });
}
/**
加载工序用料
 * **/
function loadZhYMaterial(plCode,gxCode) {
    var json = {
        plCode: plCode,
        gxCode: gxCode
    }
    layui.use('table', function () {

        var dUrl = "../Plan/GetDataList?dataType=gxzym&condition=" + JSON.stringify(json);
        var table = layui.table;
        table.render({
            elem: '#gxmlist'
            , cols: [[ //标题栏

                { field: 'proName', align: 'center', title: '材料名称', width: '15%' }
                , { field: 'spec', align: 'center', title: '材料规格', width: '14%' }
                , { field: 'cu_batch', align: 'center', title: '批次号', width: '10%' }
                , { field: 'zyNumber', align: 'center', title: '数 量', width: '12%' }
                , { field: 'slNum', align: 'center', title: '剩 料', width: '12%', edit: 'text' }
                //  , { field: 'vendorName', align: 'center', title: '供方名称', width: '18%' }

            ]], done: function (res, curr, count) {
                $("[data-field='id']").css('display', 'none');//隐藏列
                currPage = curr;
                TablePowerHide();
            }
            //   , height: $(document).height() - $('#list').offset().top - 20
            , url: dUrl
            , skin: 'row' //表格风格
            , even: true
            , page: { layout: ['count', 'prev', 'page', 'next', 'skip', 'limit'], groups: 1, first: false, last: false }
            , page: false //是否显示分页
            , limits: [20, 35, 50]
            , limit: 20 //每页默认显示的数量
            , id: "gxmlist"
        });
        table.on('edit(gxmlist)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data;
            SaveShengLiao(data.id, obj.value);
        });

    });
}